<?php

ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('memory_limit', '1024M');

/**
 * 淘宝联盟订单 没五分钟跑一次 biz-admin 机器
 */
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\wiki\Union as cacheUnion;
use Doraemon\tools\Curl;
use Doraemon\tools\Log;

$fileName = $argv[1] ?? '';
if (!empty($fileName)) {
    main($fileName);
} else {
    main();
    /*
    for ($i = 0; $i < 12; $i++) {
        var_dump('执行第' . ($i + 1) . '次');
        main();
        sleep(5 * 60);
    }
    */
}
function main($fileName = '')
{
    if (!$fileName) {
        $fileName = cacheUnion::get();
    }
    if (empty($fileName)) {
        var_dump('没有新的文件');
        return;
    }

    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
    $workSheet = $spreadsheet->getActiveSheet();
    $sheetData = $workSheet->toArray(null, true, true, true);
    $rows = $workSheet->getHighestRow();
    $highestColumn = $workSheet->getHighestColumn();
    $highestColumnNum = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $Arr = [
        "点击时间" => "click_time",
        "创建时间" => "tk_create_time",
        "付款时间" => "tk_paid_time",
        "结算时间" => "tk_earning_time",
        "淘宝付款时间" => "tb_paid_time",
        "商品ID" => "item_id",
        "商品图片" => "item_img",
        "商品标题" => "item_title",
        "掌柜旺旺" => "seller_nick",
        "店铺名称" => "seller_shop_title",
        "商品数量" => "item_num",
        "商品单价" => "item_price",
        "类目名称" => "item_category_name",
        "淘宝订单编号" => "trade_parent_id",
        "淘宝子订单号" => "trade_id",
        "订单状态" => "tk_status",
        "订单类型" => "order_type",
        "付款金额" => "alipay_total_price",
        "结算金额" => "pay_price",
        "佣金比率" => "total_commission_rate",
        "佣金金额" => "total_commission_fee",
        "补贴比率" => "subsidy_rate",
        "补贴金额" => "subsidy_fee",
        "补贴类型" => "subsidy_type",
        "收入比率" => "income_rate",
        "分成比率" => "pub_share_rate",
        "提成" => "tk_total_rate",
        "技术服务费率" => "alimama_rate",
        "技术服务费" => "alimama_share_fee",
        "付款预估收入" => "pub_share_pre_fee",
        "结算预估收入" => "pub_share_fee",
        "产品类型" => "flow_source",
        "推广者身份" => "tk_order_role",
        "媒体ID" => "site_id",
        "媒体名称" => "site_name",
        "推广位ID" => "adzone_id",
        "推广位名称" => "adzone_name",
        "成交平台" => "terminal_type",
        "维权标签" => "refund_tag",
        "内容专项服务费率" => "tk_commission_rate_for_media_platform",
        "预估内容专项服务费" => "tk_commission_pre_fee_for_media_platform",
        "结算内容专项服务费" => "tk_commission_fee_for_media_platform",
        "渠道关系ID" => "relation_id",
        "会员运营ID" => "special_id",
        "定金付款时间" => "tk_deposit_time",
        "定金淘宝付款时间" => "tb_deposit_time",
        "定金付款金额" => "deposit_price",
        "口碑父订单号" => "alsc_pid",
        "口碑子订单号" => "alsc_id",
        "渠道专项服务费率" => "share_relative_rate",
        "预估渠道专项服务费" => "share_pre_fee",
        "结算渠道专项服务费" => "share_fee",
        "团长ID" => "",
        "团长昵称" => "",
    ];
    $mapIndex = [];
    for ($j = 0; $j < $highestColumnNum; $j++) {
        $cellName = PHPExcel_Cell::stringFromColumnIndex($j) . 1;
        $cellVal = $workSheet->getCell($cellName)->getValue();//取得列内容
        if (isset($Arr[$cellVal]) && $Arr[$cellVal]) {
            $mapIndex[$j] = $Arr[$cellVal];
        }
    }
    var_dump('excel共：' . $rows . '行');
    $num = 0;
    for ($i = 2; $i <= $rows; $i++) {
        $data = [];
        for ($j = 0; $j < $highestColumnNum; $j++) {
            if (isset($mapIndex[$j])) {
                $cellName = PHPExcel_Cell::stringFromColumnIndex($j) . $i;
                $cellVal = $workSheet->getCell($cellName)->getValue();//取得列内容
                $data[$mapIndex[$j]] = !empty($cellVal) ? $cellVal : '';
            }
        }
        sendData($data);
        $num++;
    }
    var_dump('发送文件#' . $fileName . '#数据完毕,共' . $num . '条');
}


function sendData($data)
{
    $url = 'http://logcollector.hhzbi.com/v1/text/rebate_order_data';
    $params = [
        'body' => json_encode(['type' => 1, 'data' => $data]),
        'headers' => [
            'Accept' => 'text/plain;charset=utf-8',
            'Content-Type' => 'application/json; charset=utf-8',
        ]
    ];
    $result = Curl::Request('POST', $url, $params);
    if ($result['httpCode'] != 200 || $result['code'] != 1) {
        Log::debug('发送数据失败#' . $url, $result);
    }
}







